# coding=utf-8
# Date: 2021/9/22 9:26
from typing import List
from MyLinkedList import *


class Solution:
    def splitListToParts(self, head: ListNode, k: int) -> List[ListNode]:
        n = 0
        p = head
        while p is not None:
            p = p.next
            n += 1
        res = []
        p = head
        for i in range(k):
            length = n // k
            if i + 1 <= n % k:
                length += 1
            res.append(p)
            for _ in range(length - 1):
                p = p.next
            if p is not None:
                q = p.next
                p.next = None
                p = q
        return res


# 执行用时：36 ms, 在所有 Python3 提交中击败了76.36%的用户
# 内存消耗：15.5 MB, 在所有 Python3 提交中击败了27.27%的用户
if __name__ == "__main__":
    head = MyLinkedList().createLinkedList([1, 2, 3])
    res = Solution().splitListToParts(head, 5)
    for t in res:
        MyLinkedList().printLinkedList(t)
